package com.appkudos.view;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.appkudos.controller.UserController;
import com.appkudos.model.Group;
import com.appkudos.model.User;
import com.appkudos.view.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class App extends Activity implements ViewBinder{
	ListView lv;
	String appName;
	String appDesc;
	private Activity PARENTACTIVITY;
	User user;
	//User friend;
	ArrayList<Group> groups;
	UserController uc;
	Button recBtn;
		
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set content view to apps_view xml
        setContentView(R.layout.view_app); 
        PARENTACTIVITY = this;
        
        TextView appNameText = (TextView) findViewById(R.id.app_app_name);
        TextView appDescText = (TextView) findViewById(R.id.app_app_description);
        recBtn = (Button) findViewById(R.id.app_recommend_btn);
        
        recBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {     
            	// hook button to Write Review screen
            	Intent intent = new Intent(PARENTACTIVITY, WriteReviewActivity.class);
        	    intent = uc.passCurrentUser(intent, user);
                intent.putExtra("app_name", appName);
        	    startActivity(intent);
            }
          });  
        
        Intent launchIntent = getIntent();
        Bundle extras = launchIntent.getExtras();
        uc = new UserController();
        user = uc.getCurrentUser(extras);
          
        if(extras !=null)
        {
        	appName = extras.getString("app_name");
        	appNameText.setText(appName);
        	
        	appDesc = extras.getString("app_desc");
        	appDescText.setText(appDesc);
        	
        }else{
        	appNameText.setText("N/A");
        	appDescText.setText("N/A");
        }

        // only display "Recommend" button when the app has been installed
        // check if app exists in installed apps list
        List<com.appkudos.model.App> installedApps = uc.getAppsForUser(user.getUsername());
        com.appkudos.model.App app;
        boolean installed = true;
        for(int i=0; i<installedApps.size();i++) {
        	app = (com.appkudos.model.App) installedApps.get(i);
        	// if app in list does not have same id as app clicked, don't display "Recommend" button
        	if (app.getAppid() == (extras.getString("app_id"))) {
        		installed = false;
        		//Toast.makeText(getApplicationContext(), app.getAppid() + " " + extras.getString("app_id") ,Toast.LENGTH_SHORT).show();
        	  	  
        	}
        }
       
        // hide "Recommend" button
        if (installed == false) {
        	recBtn.setVisibility(View.GONE);
        } 
        
        //initGroupsList();
    }
	
	public void initGroupsList() {
        
        
        // get list view from layout
        lv=(ListView)findViewById(R.id.app_my_group_list);
        
		// get the groups you belong to  	   
  	    groups = (ArrayList<Group>) uc.getGroupsOfUser(user.getUsername());

  	   
  	    // display friends
  	    displayMatchedGroups(groups);
  	    
  	    //Toast.makeText(getApplicationContext(), "I have " + groups.size() + " groups",Toast.LENGTH_SHORT).show();
  	  
		lv.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		      Group grp = groups.get(position);
		      // When a group is clicked, go to the write review screen and populate the "Group" field
		      Toast.makeText(getApplicationContext(), grp.getGroupName()+" is clicked",Toast.LENGTH_SHORT).show();
		     
		      Intent intent = new Intent(PARENTACTIVITY, WriteReviewActivity.class);
  		      intent = uc.passCurrentUser(intent, user);
              intent = uc.passCurrentGroup(intent, grp);
              intent.putExtra("app_name", appName);
  		      startActivity(intent);
		    }
		});
		
        // enable filter on text
  	    lv.setTextFilterEnabled(true);
  	    
	}
	
	private void displayMatchedGroups(List list){
	  	Map<String, Group> itemData;
	  	Group matched;
	  	List<Map<String, Group>> listData = new ArrayList<Map<String, Group>>();
	  	
	  	// put the friends into a hashmap (used for input by simple adapter)
	  	for(int j=0; j<list.size();j++){
	  		itemData = new HashMap<String, Group>(1);
	  		matched = (Group) list.get(j);
	  		itemData.put("single_list_item", matched);
	  		listData.add(itemData);
	  	}    
	
	  	// bind simple adapter to the list view
	      SimpleAdapter simpleAdapter = new SimpleAdapter(this, listData, R.layout.single_text_item,
	              new String[] {
	                  "single_list_item"
	              }, new int[] {
	                  R.id.single_list_item
	              });
	      
	      // bind viewBinder to adapter and set adapter to listview
	      simpleAdapter.setViewBinder(this);
	      lv.setAdapter(simpleAdapter);
	  }

	@Override
	public boolean setViewValue(View view, Object data, String textRepresentation) {
		// get data for the row
		Group matched = (Group) data;
		TextView tv = (TextView) view;
  
		// bind the matched user to textView
		tv.setText(matched.getGroupName());

		return true;
	}

}

